package com.fin.quant.domain.repository.impl;

import com.fin.quant.domain.entity.AIUserSettingsEntity;
import com.fin.quant.domain.mapstruct.AiUserSettingsMapperStruct;
import com.fin.quant.domain.repository.AIUserSettingsRepository;
import com.fin.quant.instra.mysql.mapper.AiUserSettingsMapper;
import com.fin.quant.instra.mysql.model.AiUserSettings;
import com.fin.quant.instra.mysql.model.AiUserSettingsExample;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * AI用户设置服务
 */
@Service
@Transactional
@Slf4j
public class AIUserSettingsRepositoryImpl implements AIUserSettingsRepository {


    @Autowired
    private AiUserSettingsMapper settingsMapper;

    /**
     * 获取用户设置
     */
    @Override
    public AIUserSettingsEntity getUserSettings(Long userId) {
        AIUserSettingsEntity settingsEntity = findByUserId(userId);
        return settingsEntity != null ?
                settingsEntity
                : createDefaultSettings(userId);
    }

    /**
     * 更新用户设置
     */
    @Override
    public AIUserSettingsEntity updateUserSettings(Long userId, AIUserSettingsEntity settings) {
        AIUserSettingsEntity settingsEntity = findByUserId(userId);

        if (settingsEntity != null) {
            settingsEntity.setAutoDetectContext(settings.getAutoDetectContext());
            settingsEntity.setShowSuggestions(settings.getShowSuggestions());
            settingsEntity.setTheme(settings.getTheme());
            settingsEntity.setLanguage(settings.getLanguage());
            settingsEntity.setUpdatedAt(LocalDateTime.now());
            return update(settingsEntity);
        } else {
            settings.setUserId(userId);
            settings.setCreatedAt(LocalDateTime.now());
            settings.setUpdatedAt(LocalDateTime.now());
            return save(settings);
        }
    }

    /**
     * 创建默认设置
     */
    private AIUserSettingsEntity createDefaultSettings(Long userId) {
        AIUserSettingsEntity settings = new AIUserSettingsEntity();
        settings.setUserId(userId);
        settings.setAutoDetectContext(true);
        settings.setShowSuggestions(true);
        settings.setTheme(AIUserSettingsEntity.Theme.DARK);
        settings.setLanguage("zh-CN");
        settings.setCreatedAt(LocalDateTime.now());
        settings.setUpdatedAt(LocalDateTime.now());

        AiUserSettings aiUserSettings = AiUserSettingsMapperStruct.INSTANCE.entity2Model(settings);
        settingsMapper.insertSelective(aiUserSettings);
        return settings;
    }

    private AIUserSettingsEntity findByUserId(Long userId) {
        AiUserSettingsExample example = new AiUserSettingsExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<AiUserSettings> aiUserSettings = settingsMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(aiUserSettings) ?
                AiUserSettingsMapperStruct.INSTANCE.model2Entity(aiUserSettings.get(0))
                : createDefaultSettings(userId);
    }

    private AIUserSettingsEntity save(AIUserSettingsEntity settings) {
        AiUserSettings aiUserSettings = AiUserSettingsMapperStruct.INSTANCE.entity2Model(settings);
        settingsMapper.insertSelective(aiUserSettings);
        settings.setId(aiUserSettings.getId());
        return settings;
    }

    private AIUserSettingsEntity update(AIUserSettingsEntity settings) {
        AiUserSettings aiUserSettings = AiUserSettingsMapperStruct.INSTANCE.entity2Model(settings);
        settingsMapper.updateByPrimaryKeySelective(aiUserSettings);
        return settings;
    }
}
